package com.hp.bon.sgw.ctrl;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 独立系统，负责监控SGW其他进程的状态
 * 
 * @author wuzhih
 * 
 */
@Path("/")
public class SGWController
{
  private SGWRemoteInterface localHostSGWRemoteServer = ControllerConfigService.getInstance().getSgwRemoteServer();
  private static final Logger logger = LoggerFactory.getLogger(SGWController.class);
  private static final Logger sysparamLogger = LoggerFactory.getLogger("SYS_PARAM_UPDATE_LOGGER");

	@GET
	@Path("sgwparam/set/{paramValue:.*}/{node}")
	@Produces(MediaType.APPLICATION_JSON)
  	public String doParamValueSet(@PathParam("paramValue") String paramValue, @PathParam("node") String node) { try { String result = this.localHostSGWRemoteServer.doParamValueSet(paramValue, node);
      sysparamLogger.info("param set:" + result);
      return result;
    } catch (Exception e)
    {
      logger.warn("170, set param error.");
      return "error: exception " + e.toString();
    } } 
  @GET
  @Path("sgwparam/refresh")
  @Produces({"application/json"})
  public String doParamRefresh() {
    try {
      String result = this.localHostSGWRemoteServer.doParamRefresh();
      sysparamLogger.info("param refresh:" + result);
      return result;
    } catch (Exception e) {
      logger.warn("170, Refresh param error.");
      return "error: exception " + e.toString();
    }
  }
  @GET
  @Path("sgwparam/list/{param:.*}/{node}")
  @Produces({"application/json"})
  public String doParamList(@PathParam("param") String param, @PathParam("node") String node) {
    try { return this.localHostSGWRemoteServer.doParamList(param, node);
    } catch (Exception e) {
      return "error: exception " + e.toString();
    }
  }
  @GET
  @Path("config/reloadall/{node}")
  @Produces({"text/plain;charset=utf-8"})
  public String reloadConfig(@PathParam("node") String node) {
    try { String result = this.localHostSGWRemoteServer.reloadConfig(node);
      sysparamLogger.info("reload config:" + result);
      return result;
    } catch (Exception e) {
      return "error: exception " + e.toString();
    }
  }
  @GET
  @Path("sgwstat/runtime/{node}")
  @Produces({"application/json"})
  public String doSGWRuntime(@PathParam("node") String node) {
    try { return this.localHostSGWRemoteServer.doSGWRuntime(node);
    } catch (Exception e) {
      return "error: exception " + e.toString();
    }
  }
  @GET
  @Path("sgwstat/getStatToday/{module:.*}/{node}")
  @Produces({"application/json"})
  public String getStatToday(@PathParam("module") String module, @PathParam("node") String node) {
    try { return this.localHostSGWRemoteServer.getStatToday(module, node);
    } catch (Exception e) {
      return "error: exception " + e.toString();
    }
  }
  @GET
  @Path("sgwstat/getStatFromStart/{module:.*}/{node}")
  @Produces({"application/json"})
  public String getStatFromStart(@PathParam("module") String module, @PathParam("node") String node) {
    try { return this.localHostSGWRemoteServer.getStatFromStart(module, node);
    } catch (Exception e) {
      return "error: exception " + e.toString();
    }
  }
  @GET
  @Path("sgwstat/getStatHourModule/{module:.*}/{node}")
  @Produces({"application/json"})
  public String getStatHourModule(@PathParam("module") String module, @PathParam("node") String node) {
    try { return this.localHostSGWRemoteServer.getStatHourModule(module, node);
    } catch (Exception e) {
      return "error: exception " + e.toString();
    }
  }
  @GET
  @Path("sgwstat/getStatThruput/{module:.*}/{node}")
  @Produces({"application/json"})
  public String getStatThruput(@PathParam("module") String module, @PathParam("node") String node) {
    try { return this.localHostSGWRemoteServer.getStatThruput(module, node);
    } catch (Exception e) {
      return "error: exception " + e.toString();
    }
  }
  @GET
  @Path("sgwstat/getStatQueue/{module:.*}/{node}")
  @Produces({"application/json"})
  public String getStatQueue(@PathParam("module") String module, @PathParam("node") String node) {
    try { return this.localHostSGWRemoteServer.getStatQueue(module, node);
    } catch (Exception e) {
      return "error: exception " + e.toString();
    }
  }

  @GET
  @Path("sgwcheckpoint/doCheckpoint/{node}")
  @Produces({"application/json"})
  public String doCheckpoint(@PathParam("node") String node) {
    try { return this.localHostSGWRemoteServer.doCheckpoint(node);
    } catch (Exception e) {
      return "error: exception " + e.toString();
    }
  }

  public void setLocalHostSGWRemoteServer(SGWRemoteInterface localHostSGWRemoteServer)
  {
    this.localHostSGWRemoteServer = localHostSGWRemoteServer;
  }

  public static class NodeProcessInf
  {
    public String id;
    public String cmd;
    public String processType;
    public String managerURL;
    public volatile boolean processExists;
    public volatile boolean inLocalHost;
    public volatile int status;
    public volatile long statusTime;

    public NodeProcessInf(String id, String processType, String cmd, String managerURL)
    {
      this.id = id;
      this.cmd = cmd;
      this.processType = processType;
      this.managerURL = managerURL;
    }

    public String toString() {
      return "Process{id=" + this.id + ",cmd=" + this.cmd + ",managerURL=" + this.managerURL + "}";
    }

    public void setStatus(int newStatus) {
      this.status = newStatus;
      this.statusTime = System.currentTimeMillis();
    }
  }
}
